﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Plate
{
    public partial class CharacterSave : Form
    {
        //int idx;
        Bitmap character;

        static int count = 0;
        public CharacterSave(Bitmap sign)
        {
            this.character = sign;
            InitializeComponent();
            count++;
            System.Console.Write(count);
        }

        private void CharacterSave_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try 
            {
                int []oldname=new int[100];
                int Num=0;
                int newname=0;
                int k;
                string charpath = this.textBox1.Text.Trim();
                string filepath = "D:\\CVITS\\Program\\GaborRec\\Training\\"+charpath ;

                DirectoryInfo souse = new DirectoryInfo(filepath);
                FileInfo[] trainchar = souse.GetFiles();
                foreach (FileInfo var in trainchar)
                {
                    if (var.Extension.ToLower() == "bmp")
                    {
                        string sn = var.Name;
                        char[] delimitchar = { '.', '\\' };
                        string[] ss = sn.Split(delimitchar);
                        string str = ss[ss.Length - 2];
                        oldname[Num] = Convert.ToInt32(str);
                        Num++;
                    }                  
                }
                for (int i = 0; i < Num; i++)
                    for (int j = i; j < Num; j++)
                    {
                        if (oldname[i] > oldname[j])
                        {
                            k = oldname[i];
                            oldname[i] = oldname[j];
                            oldname[j] = k;
                        } 
                    }
                for (int m = 0; m < Num; m++)
                {
                    if (newname == oldname[m])
                    {
                        newname++;
                    }
                }
                string savepath = filepath + "\\" + newname + ".bmp";
                character.Save(savepath);
                this.Close();
            }
            catch (System.Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        
        }
    }
}
